<template>
  <el-row style="width:100%">
    <el-col :span="20">{{ treeNode.name }}</el-col>
    <el-col :span="4">
      <el-row>
        <el-col :span="12">{{ treeNode.manager }}</el-col>
        <el-col :span="12">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item command="edit" v-if="!isRoot"
                >查看部门</el-dropdown-item
              >

              <el-dropdown-item command="del" v-if="!isRoot"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepts } from '@/api/departments'
export default {
  props: {
    // 代表父传过来的属性名叫treeNode 相当于：props:['treeNode']
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      // 设置它的默认值
      default: false
    }
  },
  methods: {
    handleCommand (cmd) {
      // switch对于固定的值,比if的效力更高
      switch (cmd) {
        case 'add':
          // 做新增
          this.$emit('addDept', this.treeNode.id)
          break
        case 'edit':
          // 做修改
          this.$emit('editDept', this.treeNode.id)

          break
        case 'del':
          // 做删除
          // 先弹出一个确认框
          this.$confirm('确定要删除吗?', {
            confirmButtonText: '确定',
            cancelButtonText: '取消'
          }).then(async () => {
            await delDepts(this.treeNode.id)
            this.$message.success(this.treeNode.name + '删除成功!')
            //  再发一个请求去获取最新的部门列表
            this.$emit('delDept')
          })

          break
      }
    }
  }
}
</script>

<style></style>
